package com.backend.modules.notification.controller;

import com.backend.common.model.R;
import com.backend.modules.notification.dto.NotificationQueryDTO;
import com.backend.modules.notification.enums.NotificationGroup;
import com.backend.modules.notification.enums.NotificationType;
import com.backend.modules.notification.service.NotificationQueryService;
import com.backend.modules.notification.vo.NotificationVO;
import com.backend.modules.notification.vo.NotificationTypeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "通知管理", description = "通知管理相关接口")
@RestController
@RequestMapping("/v1/notifications")
@RequiredArgsConstructor
public class NotificationController {

    private final NotificationQueryService notificationQueryService;
    
    @Operation(summary = "分页查询通知")
    @GetMapping("/page")
    public R<Page<NotificationVO>> page(NotificationQueryDTO query, 
            @ParameterObject Pageable pageable) {
        return R.ok(notificationQueryService.page(query, pageable));
    }
    
    @Operation(summary = "获取未读通知数量")
    @GetMapping("/unread/count")
    public R<Long> countUnread() {
        return R.ok(notificationQueryService.countUnread());
    }
    
    @Operation(summary = "标记通知为已读")
    @PutMapping("/{id}/read")
    public R<Void> markAsRead(@PathVariable Long id) {
        notificationQueryService.markAsRead(id);
        return R.ok();
    }
    
    @Operation(summary = "批量标记通知为已读")
    @PutMapping("/batch/read")
    public R<Void> markAsRead(@RequestBody List<Long> ids) {
        notificationQueryService.markAsRead(ids);
        return R.ok();
    }
    
    @Operation(summary = "全部标记为已读")
    @PutMapping("/all/read")
    public R<Void> markAllAsRead() {
        notificationQueryService.markAllAsRead();
        return R.ok();
    }
    
    @Operation(summary = "删除通知")
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        notificationQueryService.delete(id);
        return R.ok();
    }
    
    @Operation(summary = "批量删除通知")
    @DeleteMapping("/batch")
    public R<Void> delete(@RequestBody List<Long> ids) {
        notificationQueryService.delete(ids);
        return R.ok();
    }
    
    @Operation(summary = "获取通知类型列表")
    @GetMapping("/types")
    public R<List<NotificationTypeVO>> getTypes(
            @Parameter(description = "通知分组") 
            @RequestParam(required = false) NotificationGroup group) {
        List<NotificationTypeVO> types = new ArrayList<>();
        
        NotificationType[] notificationTypes = group != null ?
                NotificationType.getTypesByGroup(group) : 
                NotificationType.values();
                
        for (NotificationType type : notificationTypes) {
            NotificationTypeVO vo = new NotificationTypeVO();
            vo.setCode(type.name());
            vo.setDescription(type.getDescription());
            vo.setGroup(type.getGroup().getDescription());
            types.add(vo);
        }
        
        return R.ok(types);
    }
} 